package org.jaaksi.test.jpush.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import org.jaaksi.test.jpush.R;
import org.jaaksi.test.jpush.base.BaseActivity;
import org.jaaksi.test.jpush.model.push.MsgPushFeedBean;
import org.jaaksi.test.jpush.util.ConstantUtils;
import org.jaaksi.test.jpush.util.EventBusTool;
import org.jaaksi.test.jpush.util.SpFactory;
import org.simple.eventbus.Subscriber;

/**
 * Created by Fcy on 2016/1/24. 消息列表页，这里应该是个listview，简单用两个view显示 包括房源和客源
 */
public class MessageListActivity extends BaseActivity
        implements View.OnClickListener
{
	private TextView tv_tip_customer, tv_tip_house;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_message_list);
		EventBusTool.register(this);
		initView();
		initPoint();
	}

	private void initView()
	{
		findViewById(R.id.tab_customer).setOnClickListener(this);
		findViewById(R.id.tab_house).setOnClickListener(this);

		tv_tip_customer = findView(R.id.tv_tip_customer);
		tv_tip_house = findView(R.id.tv_tip_house);
	}

	@Subscriber(tag = ConstantUtils.EventTag.UPDATE_SOURCE_HOUSE)
	public void updateHouse(String msg)
	{ // 可不可以多个标签，然后判断标签时那个
		initPoint();
	}

	@Subscriber(tag = ConstantUtils.EventTag.UPDATE_SOURCE_CUSTOMER)
	public void updateCustomer(String msg)
	{
		initPoint();
	}

	// // 没有必要更新所有，只更新对应的即可
	// @Subscriber(tag = ConstantUtils.EventTag.UPDATE_POINT)
	// public void update(String msg)
	// {
	// initPoint();
	// }

	/**
	 * 初始化小红点
	 */
	private void initPoint()
	{
		// 客源
		MsgPushFeedBean customerPushFeedBean = SpFactory.getInstance(this)
		        .getCustomerPushFeedData();
		if (customerPushFeedBean != null)
		{
			int count = customerPushFeedBean.unReadErShou
			        + customerPushFeedBean.unReadNewHouse;
			if (count == 0)
			{
				tv_tip_customer.setVisibility(View.GONE);
			}
			else
			{
				tv_tip_customer.setVisibility(View.VISIBLE);
				tv_tip_customer.setText(count + "");
			}
		}

		// 房源
		MsgPushFeedBean housePushFeedBean = SpFactory.getInstance(this)
		        .getHousePushFeedData();
		if (housePushFeedBean != null)
		{
			int count = housePushFeedBean.unReadErShou
			        + housePushFeedBean.unReadNewHouse;
			if (count == 0)
			{
				tv_tip_house.setVisibility(View.GONE);
			}
			else
			{
				tv_tip_house.setVisibility(View.VISIBLE);
				tv_tip_house.setText(count + "");
			}
		}

	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
			case R.id.tab_customer:
				break;
			case R.id.tab_house:
				Intent intent = new Intent(this, HouseFeedActivity.class);
				startActivity(intent);
				break;
		}
	}

	@Override
	protected void onDestroy()
	{
		EventBusTool.unregister(this);
		super.onDestroy();
	}
}